package com.kai.service.article.controller;

import com.kai.service.article.facade.ArticleFacade;
import com.kai.service.data.PagingResult;
import com.kai.service.data.Result;
import com.kai.service.data.response.article.ArticleCommentResponse;
import com.kai.service.data.response.article.ArticleDetailResponse;
import com.kai.service.data.response.article.ArticleResponse;
import com.kai.service.util.ResultResponseUtil;
import com.kai.service.data.request.article.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/article")
@Tag(name = "文章")
public class ArticleController {

    @Autowired
    private ArticleFacade articleFacade;

    /**
     * 新增文章
     *
     * @param createArticleRequest
     * @return
     */
    @PostMapping
    @Operation(summary = "新增文章")
    public Result<Boolean> createArticle(@RequestBody @Valid CreateArticleRequest createArticleRequest){
        return ResultResponseUtil.success(articleFacade.createArticle(createArticleRequest));
    }

    /**
     * 编辑文章
     *
     * @param articleId
     * @param editorArticleRequest
     * @return
     */
    @PutMapping("/{articleId}")
    @Operation(summary = "编辑文章")
    public Result<Boolean> editorArticle(@PathVariable("articleId") Long articleId, @Valid @RequestBody EditorArticleRequest editorArticleRequest){
        return ResultResponseUtil.success(articleFacade.editorArticle(articleId, editorArticleRequest));
    }

    /**
     * 分页查询文章
     *
     * @param pagingArticleRequest
     * @return
     */
    @PostMapping("/paging")
    @Operation(summary = "分页查询文章")
    public PagingResult<ArticleResponse> pagingArticle(@RequestBody PagingArticleRequest pagingArticleRequest){
        return ResultResponseUtil.pageSuccess(articleFacade.pagingArticle(pagingArticleRequest));
    }

    /**
     * 分页查询历史文章
     *
     * @param pagingHistoryArticleRequest
     * @return
     */
    @PostMapping("/history/paging")
    @Operation(summary = "分页查询历史文章")
    public PagingResult<ArticleResponse> historyPagingArticle(@Valid @RequestBody PagingHistoryArticleRequest pagingHistoryArticleRequest){
        return ResultResponseUtil.pageSuccess(articleFacade.historyPagingArticle(pagingHistoryArticleRequest));
    }

    /**
     * 获取热门文章
     *
     * @param userId
     * @return
     */
    @GetMapping("/hot")
    @Operation(summary = "获取热门文章")
    public Result<List<ArticleResponse>> getHotArticle(@RequestParam("userId") Long userId){
        return ResultResponseUtil.success(articleFacade.getHotArticle(userId));
    }

    /**
     * 查看文章内容
     *
     * @param articleId
     * @return
     */
    @GetMapping("/content/{articleId}")
    @Operation(summary = "查看文章内容")
    public Result<ArticleDetailResponse> articleDetail(@PathVariable("articleId") Long articleId){
        return ResultResponseUtil.success(articleFacade.articleDetail(articleId));
    }

    /**
     * 删除文章
     *
     * @param articleId
     * @return
     */
    @DeleteMapping("/{articleId}")
    @Operation(summary = "删除文章")
    public Result<Boolean> deleteArticle(@PathVariable("articleId") Long articleId){
        return ResultResponseUtil.success(articleFacade.deleteArticle(articleId));
    }

    /**
     * 文章操作
     *
     * @param request
     * @return
     */
    @PutMapping("/{userId}/operate/{articleId}")
    @Operation(summary = "文章操作")
    public Result<Boolean> articleOperate(@Valid ArticleOperatorRequest request){
        return ResultResponseUtil.success(articleFacade.articleOperate(request));
    }

    /**
     * 新增评论
     *
     * @param articleId
     * @param articleCommentRequest
     * @return
     */
    @PostMapping("/comment/{articleId}")
    @Operation(summary = "新增评论")
    public Result<Boolean> articleComment(@PathVariable("articleId") Long articleId, @Valid @RequestBody ArticleCommentRequest articleCommentRequest){
        return ResultResponseUtil.success(articleFacade.articleComment(articleId, articleCommentRequest));
    }

    /**
     * 分页查看文章评论
     *
     * @param articleId
     * @param pagingArticleCommentRequest
     * @return
     */
    @PostMapping("/comment/{articleId}:paging")
    @Operation(summary = "分页查看文章评论")
    public PagingResult<ArticleCommentResponse> pagingArticleComment(@PathVariable("articleId") Long articleId, @Valid @RequestBody PagingArticleCommentRequest pagingArticleCommentRequest){
        return ResultResponseUtil.pageSuccess(articleFacade.pagingArticleComment(articleId, pagingArticleCommentRequest));
    }

    /**
     * 评论点赞
     *
     * @param userId
     * @param commentId
     * @param isLike
     * @return
     */
    @PutMapping("/like/comment/{userId}/{commentId}")
    @Operation(summary = "评论点赞")
    public Result<Boolean> likeComment(@PathVariable("userId") Long userId, @PathVariable("commentId") Integer commentId, @RequestParam("isLike") Boolean isLike){
        return ResultResponseUtil.success(articleFacade.likeComment(userId, commentId, isLike));
    }

    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @DeleteMapping("/comment/{commentId}")
    @Operation(summary = "删除评论")
    public Result<Boolean> deleteComment(@PathVariable("commentId") Integer commentId){
        return ResultResponseUtil.success(articleFacade.deleteComment(commentId));
    }
}
